/*
 * Copyright 2010 Laurent Moulinier, Christian Matzat and others
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.gafmedia.graph.example;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import com.gafmedia.graph.LineChartItem;
import com.gafmedia.graph.LineChartView;
import com.gafmedia.graph.PieChartItem;
import com.gafmedia.graph.PieChartView;

import java.util.*;

public class MainActivity extends Activity {

    //—————————————————————-
    // border constant => empty zone around the line chart
    //—————————————————————-
    private static final int CHART_LEFT_GAP = 20;
    private static final int CHART_RIGHT_GAP = 16;
    public static final int CHART_GAP_TOP = 24;
    public static final int CHART_GAP_BOTTOM = 20;


    //—————————————————————-
    // fill the graph if true, or draw only a line if false
    //—————————————————————-
    private boolean fillGraph = false;

    //—————————————————————-
    // Draw the average line if true => Sum(Y_value)/Y_value_count
    //—————————————————————-
    private boolean showAverageLine = true;

    //—————————————————————-
    // Draw a grid in the background
    //—————————————————————-
    private boolean showGrid = false;

    //—————————————————————-
    // Show the X-Data range as headline
    //—————————————————————-
    private boolean showXRangeHeadline = true;

    //—————————————————————-
    // zoom the graph between the min and max y value, else the graph is draw between 0 -> y max value
    //—————————————————————-
    private boolean zoomChart = false;

    //—————————————————————-
    // invert the scale, means show the highest value at the bottom
    //—————————————————————-
    private boolean invertYScale = true;

    //—————————————————————-
    // colors used
    //—————————————————————-
    private int backgroundColor = Color.TRANSPARENT;
    private int textColor = Color.LTGRAY;
    private int gridColor = Color.WHITE;
    private int averageLineColor = Color.RED;

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        // PieChart initialization
        List<PieChartItem> data = createPieChartData();

        PieChartView pieChartView = (PieChartView) findViewById(R.id.pie_chart);
        int size = 115;
        pieChartView.setGeometry(size, size, CHART_LEFT_GAP, CHART_RIGHT_GAP, CHART_GAP_TOP, CHART_GAP_BOTTOM, 0, true);//R.drawable.cam_overlay);
        pieChartView.setSkinParams(backgroundColor);
        int maxCount = 0;
        for (PieChartItem item : data) {
            maxCount += item.count;
        }
        pieChartView.setData(data, maxCount);
        pieChartView.invalidate();

        // LineChart initialization
        LineChartView chartView = (LineChartView) findViewById(R.id.line_chart);

        chartView.setGeometry(metrics.widthPixels, metrics.widthPixels / 2, CHART_LEFT_GAP, CHART_RIGHT_GAP, CHART_GAP_TOP, CHART_GAP_BOTTOM);
        chartView.setSkinParams("ChartLabel", backgroundColor, gridColor, textColor, averageLineColor, fillGraph, showAverageLine, showGrid, zoomChart, invertYScale, showXRangeHeadline);
        int[] lineColorArray = new int[] {Color.YELLOW, Color.WHITE, Color.CYAN, Color.LTGRAY, Color.GREEN};
        for (int i=0 ; i<3; i++) {
            List<LineChartItem> lineChartItems = createLineChartData();
            chartView.addData(lineChartItems, lineColorArray[i], true);
        }
        chartView.invalidate();
    }


    private List<LineChartItem> createLineChartData() {

        Map<String, Integer> lineChartData = createDummyLineChartData();
        List<LineChartItem> lineChartItems = new ArrayList<LineChartItem>();
        TreeSet<String> keySet = new TreeSet<String>(lineChartData.keySet());
        for (String date : keySet) {
            LineChartItem chartItem = new LineChartItem();
            String[] splitDate = date.split("-");
            chartItem.entry = splitDate[0] + "-" + splitDate[1];
            chartItem.value = lineChartData.get(date);
            lineChartItems.add(chartItem);
        }
        return lineChartItems;

    }

    private Map<String, Integer> createDummyLineChartData() {
        Random mNumGen = new Random();
        Map<String, Integer> lineChartData = new HashMap<String, Integer>();
        lineChartData.put("01-10-2010", mNumGen.nextInt(20));
        lineChartData.put("02-10-2010", mNumGen.nextInt(20));
        lineChartData.put("03-10-2010", mNumGen.nextInt(20));
        lineChartData.put("04-10-2010", mNumGen.nextInt(20));
        lineChartData.put("05-10-2010", mNumGen.nextInt(20));
        lineChartData.put("06-10-2010", mNumGen.nextInt(20));
        lineChartData.put("07-10-2010", mNumGen.nextInt(20));
        lineChartData.put("08-10-2010", mNumGen.nextInt(20));
        lineChartData.put("09-10-2010", mNumGen.nextInt(20));
        lineChartData.put("10-10-2010", mNumGen.nextInt(20));
        lineChartData.put("11-10-2010", mNumGen.nextInt(20));
        lineChartData.put("12-10-2010", mNumGen.nextInt(20));
        lineChartData.put("13-10-2010", mNumGen.nextInt(20));
        lineChartData.put("14-10-2010", mNumGen.nextInt(20));
        lineChartData.put("15-10-2010", mNumGen.nextInt(20));
        lineChartData.put("16-10-2010", mNumGen.nextInt(20));
        lineChartData.put("17-10-2010", mNumGen.nextInt(20));
        lineChartData.put("18-10-2010", mNumGen.nextInt(20));
        lineChartData.put("19-10-2010", mNumGen.nextInt(20));
        lineChartData.put("20-10-2010", mNumGen.nextInt(20));
        lineChartData.put("21-10-2010", mNumGen.nextInt(20));
        lineChartData.put("22-10-2010", mNumGen.nextInt(20));
        lineChartData.put("23-10-2010", mNumGen.nextInt(20));
        lineChartData.put("24-10-2010", mNumGen.nextInt(20));
        lineChartData.put("25-10-2010", mNumGen.nextInt(20));
        lineChartData.put("26-10-2010", mNumGen.nextInt(20));
        lineChartData.put("27-10-2010", mNumGen.nextInt(20));
        lineChartData.put("28-10-2010", mNumGen.nextInt(20));
        return lineChartData;
    }

    private List<PieChartItem> createPieChartData() {
        List<PieChartItem> data = new ArrayList<PieChartItem>(0);

        Random mNumGen = new Random();
        int MaxPieItems = mNumGen.nextInt(20);

        for (int i = 0; i < MaxPieItems; i++) {
            int itemcount = mNumGen.nextInt(256);
            PieChartItem pieChartItem = new PieChartItem();
            pieChartItem.count = itemcount;
            pieChartItem.label = "Valeur " + mNumGen.nextInt(1000);
            pieChartItem.color = 0xff000000 + 256 * 256 * mNumGen.nextInt(256) + 256 * mNumGen.nextInt(256) + mNumGen.nextInt(256);
            data.add(pieChartItem);
        }
        return data;
    }

}
